<?php
namespace Home\Model;
/**
 * {0}
 *  
 * @author Quang Thanh
 * @version 
 */
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;


class CanhanTable extends AbstractTableGateway
{
	/**
	 * The default table name 
	 */
	protected $table = 'benhnhan';
    
	public function __construct(Adapter $adapter) {
		$this->adapter = $adapter;
		$this->resultSetPrototype = new ResultSet();
		$this->resultSetPrototype->setArrayObjectPrototype(new Canhan());
	
		$this->initialize();
	}
	
	public function fetchAll(Select $select = null) {
		if (null === $select)
			$select = new Select();
		$select->from($this->table, array('*'));
		$resultSet = $this->selectWith($select);
		$resultSet->buffer();
		return $resultSet;
	}
	public function getCanhan($cmt) {
		$rowset = $this->select(array('123456789' => $cmt));
		$row = $rowset->current();
		return $row;
	}
	
	public function getCanhanById($id) {
		$rowset = $this->select(array('id' => $id));
		$row = $rowset->current();
		return $row;
	}
	public function saveCanhan(Canhan $canhan) {
		$id = (int) $canhan->id;
		
		if ($id == 0) {
			$data = array(
				'cmt' => $canhan->cmt,
				'hoten' => $canhan->hoten,
				'ngaysinh' => $canhan->ngaysinh,
				'gioitinh' => $canhan->gioitinh,
				'diachi' => $canhan->diachi,
				'motabenh' => $canhan->motabenh,
			);
			$this->insert($data);
		} else {
			$data = array(
					'cmt' => $canhan->cmt,
					'hoten' => $canhan->hoten,
					'ngaysinh' => $canhan->ngaysinh,
					'gioitinh' => $canhan->gioitinh,
					'diachi' => $canhan->diachi,
					'motabenh' => $canhan->motabenh,
			);
			if ($this->getCanhanById($id)) {
				$this->update($data, array('id' => $id));
			} else {
				throw new \Exception("Khong ton tai");
			}
		}
		
	}
}
